﻿using System;
using System.Collections.Generic;

namespace UDPHelper
{
    /// <summary>
    /// 管道
    /// （用于组装复杂的判定逻辑）
    /// </summary>
    public abstract class Pipeline
    {
        /// <summary>
        /// 子集管道
        /// </summary>
        public List<Pipeline> Childs = new List<Pipeline>();

        /// <summary>
        /// 处理逻辑
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public abstract bool Handle(object context);
    }

    /// <summary>
    /// 选择管道
    /// </summary>
    public class SelectPipeline : Pipeline
    {
        public override bool Handle(object context)
        {
            bool success = false;

            foreach (var item in Childs)
            {
                if (item.Handle(context))
                {
                    success = true;
                    break;
                }
            }

            return success;
        }
    }

    /// <summary>
    /// 顺序管道
    /// </summary>
    public class SequencePipeline : Pipeline
    {
        public override bool Handle(object context)
        {
            bool success = true;

            foreach (var item in Childs)
            {
                if (!item.Handle(context))
                {
                    success = false;
                    break;
                }
            }

            return success;
        }
    }

    /// <summary>
    /// 反选管道
    /// </summary>
    public class NotPipeline : Pipeline
    {
        public override bool Handle(object context)
        {
            if (Childs.Count == 0)
                return false;

            return !Childs[0].Handle(context);
        }
    }
}